package com.zhentao.service.impl;

import com.zhentao.common.service.FileStorageService;
import com.zhentao.mapper.MaBannerMapper;
import com.zhentao.model.common.app.pojo.Banner;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.MaBannerService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 王越潇
 * @version 1.0
 * @Date 2024/9/20 19:42
 * @Type
 */
@Service
public class MaBannerServiceImpl implements MaBannerService {
    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private MaBannerMapper maBannerMapper;
    @Override
    public ResponseResult findAll() {
        List<Banner> banners = maBannerMapper.selectList(null);
        return ResponseResult.okResult(banners);
    }

    @Override
    public ResponseResult add(Banner banner) {
        maBannerMapper.insert(banner);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult update(Banner banner) {
        maBannerMapper.updateById(banner);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult delete(Integer id) {
        maBannerMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public String file(MultipartFile file) throws IOException {
        String filename= UUID.randomUUID()+file.getOriginalFilename();
        String path = fileStorageService.uploadImage(filename, file.getInputStream());
        return path;
    }

}
